package com.baosight.sczx.zp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiBlockMeta;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.spring.SpringApplicationContext;
import com.baosight.iplat4j.dao.Dao;
import com.baosight.iplat4j.ef.ui.tree.TreeConstants;
import com.baosight.iplat4j.ef.ui.tree.TreeService;
import com.baosight.iplat4j.ep.QueryMap;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.CmesConstants;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zp.ZPUtils;
import com.baosight.sczx.zp.domain.ZPAuthzTree;


/**
 * 组织机构树
 * <p>Title: ServiceZP07.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2012-10-18 上午05:06:07  郭淑芳  Create
 *</pre>
 */
public class ServiceZP07 extends TreeService  {

	private static Dao dao = (Dao)SpringApplicationContext.getBean("dao");
	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午05:06:28 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.zp.orgType"));//机构分类
		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.zp.orgProperty",true));//机构性质
		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.zp.business",true));//主管专业类型
		
		return inInfo;
	}
	/**
	 * 获取根节点
	 * <p>Function: getTopNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZPTree</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-17上午09:27:34 郭淑芳
	 * </p>
	 *
	 */
	@Override 
	public List getTopNodes() {
		List ret = new ArrayList();
		
		Map map = new HashMap();
		map.put("name", ZPUtils.USER_COMPANY_ID);
		List list=dao.query("ZPAuthzTree.query", map);
		if(list.size()>0)
		{
			ZPAuthzTree org = (ZPAuthzTree)list.get(0);
			Map row = new HashMap();
			row.put("attribute", org.getAttribute());			// 是否启用
			row.put("parentName", org.getParentName());			// 上级节点ID
			row.put("text", org.getName());
			row.put("label", org.getDisplayName());
			row.put("leaf", 0);
			ret.add(row);
		}
		else
		{
			map.put("name", ZPUtils.USER_COMPANY_ID); 	//机构ID
			map.put("displayName", "煤炭公司");			//机构名称
			map.put("sortIndex", 1);					//是否生产部门
			map.put("attribute", "0");					//是否启用
			map.put("ext3", "00");						//机构编号
			map.put("property", "矿区");					//机构分类
			map.put("parentName", "");					//上级节点ID
			dao.insert("ZPAuthzTree.insert", map);
			
			Map row = new HashMap();
			row.put("text", ZPUtils.USER_COMPANY_ID);
			row.put("label", "煤炭公司");	
			row.put("attribute", "0");	
			row.put("parentName", "");	
			row.put("leaf", "0");
			ret.add(row);
		}
				
		return ret;
	}
	
	/**
	 * 机构树的查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午09:54:15 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo query(EiInfo inInfo) {
		QueryMap queryInfo = QueryMap.getQueryMap(inInfo);
		String p = (String) queryInfo.get(TreeConstants.TREE_NODE);
		List children = new ArrayList();
		if (p == null) {
			p = "$";
		}
		if ("$".equals(p)) {
			children = this.getTopNodes();
		} else {
			children = this.getChildNodes(p);
		}

		EiInfo outInfo = new EiInfo();
		EiBlock block = outInfo.addBlock(p);
		block.setBlockMeta(initMetaData());
		block.setRows(children);
		return outInfo;
	}

	/**
	 * 系统模块树结构：构造查询树节点的对应子节点
	 * <p>Function: getChildNodes</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午05:15:42 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List getChildNodes(String args) {
		Map _argMap = new HashMap();
		_argMap.put(TreeConstants.TREE_NODE, args);  //树节点过滤参数     此树是通过"父类ID"来建立的上下关系

		List childNodes = dao.query("ZPAuthzTree.treeNodes", _argMap);
		return childNodes;
	}
	
	/**
	 * 
	 * <p>Function: initMetaData</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZPTree</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-17上午09:28:45 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiBlockMeta initMetaData() {
		EiBlockMeta eiMetadata = new EiBlockMeta();
		
		EiColumn eiColumn = new EiColumn("text");
		eiColumn.setDescName("text");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("label");
		eiColumn.setDescName("label");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("leaf");
		eiColumn.setDescName("leaf");
		eiColumn.setType("N");
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("parentName");
		eiColumn.setDescName("parentName");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		eiColumn = new EiColumn("attribute");
		eiColumn.setDescName("是否启用");
		eiColumn.setNullable(false);
		eiColumn.setPrimaryKey(false);
		eiMetadata.addMeta(eiColumn);
		
		return eiMetadata;
	}

	/**
	 * 叶子页面数据新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午09:54:53 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		Map<String, String> _map = new HashMap<String, String>();
	
		// 机构名称、机构编号是否重复
		boolean isRepeat = validate(inInfo, "ZPAuthzTree.query", _map);
		if(isRepeat) {
			return inInfo;
		}
		else {
			inInfo.getBlock("result").setCell(0, "name", TablePk.getInstance().getNextValue16());//主键
			inInfo.getBlock("result").setCell(0, "attribute", "0");//默认启用
			EiInfo outInfo = super.insert(inInfo, "ZPAuthzTree.insert");
			this.changeInitialData(outInfo);//更新缓存
			return outInfo;
		}
		
	}


	/**
	 * 删除机构信息
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午06:16:54 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		String name = inInfo.getString("name");
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("name", name);

		Map<String, String> _map2 = new HashMap<String, String>();
		_map2.put("orgCode", name);

		// 当前组织机构下有无子组织机构校验
		boolean isHasChildrenType = hasChildrenType(_map, inInfo);
		// 当前组织机构下有无用户校验
		boolean isHasUser = hasUser(_map2, inInfo);
		if(isHasChildrenType) {
			inInfo.setStatus(-1);
			inInfo.setMsg("当前组织机构有下属机构,请先删除下属机构！");
			return inInfo;
		} else if ( isHasUser ) {
			inInfo.setStatus(-1);
			inInfo.setMsg("当前组织机构有用户,请先删除下属用户！");
			return inInfo;
		} else {
			dao.delete("ZPAuthzTree.delete", _map);
			this.changeInitialData(inInfo);//更新缓存
			return inInfo;
			}
	}
	
	/**
	 * 判断某个机构下是否有子机构
	 * <p>Function: hasChildrenType</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午06:11:03 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean hasChildrenType(Map<String,String> _argMap, EiInfo eiInfo) {
		List<Map>  _list = new ArrayList();
		try {
			_list = dao.query("ZPAuthzTree.queryHasChildrenType", _argMap);
			if(_list.size() < 1) {
				return false;
			}
			else {
			}
		} catch (Exception e) {
			System.out.println("判断当前组织机构下是否有子机构异常：" + e.getMessage());
			return false;
		}
		
		return true;
	}	

	/**
	 * 判断某个机构下是否有用户
	 * <p>Function: hasChildrenType</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-18上午06:11:03 郭淑芳
	 * </p>
	 *
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static boolean hasUser(Map<String,String> _argMap, EiInfo eiInfo) {
		List<Map>  _list = new ArrayList();
		try {
			_list = dao.query("ZPUser.query", _argMap);
			if(_list.size() < 1) {
				return false;
			}
			else {
			}
		} catch (Exception e) {
			
		}
		
		return true;
	}	
	
	/**
	 * 机构编号、机构名称重复校验
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-22上午02:05:54 郭淑芳
	 * </p>
	 *
	 */
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		Map<String, String> params1 = new HashMap<String, String>();
		
		String name = inInfo.getBlock("result").getCellStr(0, "name");
		String displayName = inInfo.getBlock("result").getCellStr(0, "displayName");
		String ext3 = inInfo.getBlock("result").getCellStr(0, "ext3");
		
		params.put("displayName", displayName);		
		params1.put("ext3", ext3);
			
		//编辑时校验需要剔除本记录
		if(!(name==null || name.equals(""))){
			params.put("name", name);
			params1.put("name", name);
		}
	
		List result1 = dao.query(sqlName, params1);
		if(result1!=null && result1.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg(" 机构编号重复！" );
			return true;
		}
		
		List result = dao.query(sqlName, params);
		if( result!=null && result.size() > 0 ) {
			inInfo.setStatus(-1);
			inInfo.setMsg(" 机构名称重复！" );
			return true;
		}
		
		return false;
	}
	
	/**
	 * 机构 启用
	 * <p>Function: start</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午09:55:24 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo start(EiInfo inInfo) 
	{
		EiInfo outInfo = super.update(inInfo, "ZPAuthzTree.start");
		this.changeInitialData(outInfo);//更新缓存
		return outInfo;
	}
	
	/**
	 * 机构 停用
	 * <p>Function: stop</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2012-10-23上午09:55:40 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo stop(EiInfo inInfo) 
	{
		
		String name = inInfo.getCellStr("result", 0, "name");
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("name", name);

		Map<String, String> _map2 = new HashMap<String, String>();
		_map2.put("orgCode", name);

		//有无子节点校验
		boolean isHasChildrenType = hasChildrenType(_map, inInfo);
		// 该机构下有无用户校验
		boolean isHasUser = hasUser(_map2, inInfo);
		if(isHasChildrenType) {
			inInfo.setStatus(-1);
			inInfo.setMsg("当前组织机构有下属机构,不能停用该机构！");
			return inInfo;
		} else if ( isHasUser ) {
			inInfo.setStatus(-1);
			inInfo.setMsg("当前组织机构有用户,不能停用该机构！");
			return inInfo;
		} else {
			EiInfo outInfo = super.update(inInfo, "ZPAuthzTree.stop");
			this.changeInitialData(outInfo);//更新缓存
			return outInfo;
			}
	}
	
	/**
	 * 如果修改成功则刷新数据
	 * <p>Function: changeInitialData</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-18下午02:59:02 严洪浩
	 * </p>
	 *
	 */
	private void changeInitialData(EiInfo outInfo){
		if(outInfo != null && outInfo.getStatus() != -1) {
			CmesConstants.DEPARTMENT_STATUS = 1;
		}
	}
}
